import angular from 'angular' 

class OrdersService {
    constructor($http, ConfigService, AuthService) {
        this.$http = $http;
        this.host = ConfigService.apiServerUrl;
        this.authService = AuthService;
    }

    makeOrderInfoFriendly(order) {
        if (order) {
            let dic = {
                'paid': '已预付',
                'unpaid': '未支付',
                'finished': '已结束',
                'cancelled': '已取消'
            };

            order.statusText = dic[order.status];

            if (order.callbackParams && order.callbackParams.return_code && order.callbackParams.result_code) {
                order.channel = 'wechat';
            } else {
                order.channel = 'other';
            }

            if (order.channel === 'wechat') {
                order.prepay = +order.prepay / 100;
            }
        }
    }

    addOrder(booking) {
        return this.$http.post(
            this.host + '/admin/order',
            booking
        );
    }

    addOptionText(order){
        var time = order.timeRange.substring(0, 2) + ':' + order.timeRange.substring(2, 4) + '-'
            + order.timeRange.substring(4, 6) + ':' + order.timeRange.substring(6, 8);
        order.optionText = order.name + '/' + order.shopName + '/' + order.roomName + '/' + order.bookingDate + '/' + time;
    }

    getCancelledOrders(telNO) {
        return this.$http.get(this.host + '/admin/cancelledOrders/?telNO='+telNO)
            .then(res=>{
                var cancelledOrders = res.data.data;
                if (cancelledOrders) {
                    cancelledOrders.forEach(order => {
                        this.addOptionText(order);
                    });
                    return cancelledOrders;
                }
            })
            .catch(err => {
                console.error(err);
            });
    }

    getOrders() {
        return this.$http.get(this.host + '/admin/orders').then(
            res => {
                var orders = res.data.data;

                if (orders) {
                    orders.forEach(order => {
                        this.makeOrderInfoFriendly(order);
                    });
                }

                return orders
            },
            reason => {
                console.info(reason);
            }
        )
        .catch(err => {
            console.error(err);
        });
    }

    updateOrder(order) {
        var info = {
            NO: order.NO,
            outTradeNO: order.outTradeNO,
            status: order.status,
            guide: order.guide,
            groupCoupons: order.groupCoupons, 
            //coupons: order.coupons,
            payee: order.payee,
            note: order.note,
            actualPersons: order.actualPersons,
            reallyPay: +order.reallyPay,
            payWay: order.payWay,
            bookingWay: order.bookingWay,
            originOrder: order.originOrder,
            modifyRecord: order.modifyRecord
        };

        if (info.status === 'finished') {
            info.endMan = this.authService.username;
        }else if (info.status === 'cancelled') {
            info.cancelPerson = this.authService.username;
        }

        return this.$http.post(
            this.host+ '/admin/patchorder',
            info
        ).then(res => {
            return res.date;
        });
    }

    getPayWays(){
        return this.$http.get(this.host + '/admin/payWays')
        .then(res=>{
            var payWays = res.data.data;
            if (payWays) {
                return payWays;
            }
            throw new Error("can not get payWays");
        })
        .catch(err => {
            console.error(err);
        });
    }
}

OrdersService.$inject = ['$http', 'configService', 'authService'];

export default angular.module('services.orders', [])
    .service('ordersService', OrdersService)
    .name;

